package JDBC;

import java.sql.*;
import java.util.Scanner;
/*
* 创建表  插入数据，实现用户登入*/
public class Text06 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet=null;
        /*String tname;
        String password;*/
        boolean flag=true;
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入用户名");
        String name=scanner.next();
        System.out.println("输入密码：");
        String password=scanner.next();

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521:orcl", "kz", "root");
            statement = connection.createStatement();
            String sql="select * from master where username='"+name+"'  and userpassword='"+password+"'";
            //创建表
           // String sql = "create table master (userid number,username Varchar2(20),password varchar2(20))";
            /*String sql="insert into master values (1001,'Tom','123456')";
            String sql1="insert into master values(1002,'Arry','111111')";
            int i = statement.executeUpdate(sql);
             statement.executeUpdate(sql1);
            System.out.println(i);*/
             resultSet = statement.executeQuery(sql);
            if (resultSet.next()){
                System.out.println("登入成功~~~~");
              /*  if (name.equals(resultSet.getString("username"))&&password.equals(resultSet.getString("userpassword"))){

                }*/
            }else {
                System.out.println("登入失败！！！");
            }
           /* while (resultSet.next()){
                 tname=resultSet.getString("username");
                 password=resultSet.getString("userpassword");
               // System.out.println(tname+"   "+password);

                if (username.equals(tname)&&userpassword.equals(password)){
                    System.out.println("登入成功");
                    flag=false;
                }
            }
            if (flag){
                System.out.println("登入失败！");
            }*/

        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            try {
                resultSet.close();
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
